import { ADD_PERSON } from "../constant";

const initState = [{ id: "001", name: "tom", age: 18 }];
export default function personReducer(prevState, action) {
    const { type, data } = action;

    switch (type) {
        case ADD_PERSON:
            return [data, ...prevState];
        default:
            // 初始化时，传递的prevState为undefined，此时返回initState
            if (prevState === undefined) {
                return initState;
            }
            // 为保证combineReducers()函数合并后的reducer正确，必须返回prevState
            return prevState;
    }
}
